package com.bwhx.eruler.controller.casdilly;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.bean.casdilly.ClothesType;
import com.bwhx.eruler.bean.casdilly.Contract;
import com.bwhx.eruler.bean.casdilly.ContractStatus;
import com.bwhx.eruler.controller.BaseController;
import com.bwhx.eruler.service.casdilly.ClothesTypeService;
import com.bwhx.eruler.service.casdilly.ContractService;
import com.bwhx.eruler.util.MD5Utils;
import com.bwhx.eruler.util.StringUtils;


@Controller
@RequestMapping("/contract")
public class ContractController extends BaseController {
	
	@Autowired
	ContractService contractService;
	
	@Autowired
	ClothesTypeService clothesTypeService;
	
 
	
	@ResponseBody
	@RequestMapping(value = "/page.list.ashx")
	 public Map<String,Object> contractList(Integer page,Integer rows,Contract contract) {
		

		
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,contract);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			List<Contract> list = contractService.list(p);
			Integer count = contractService.count(contract);

					
			resultMap.put("total",count );//total键 存放总记录数，必须的  
			resultMap.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			resultMap.put("msg", "失败！");
			resultMap.put("status", "0");
		}
		
	    return resultMap;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.saveOrUpdate.ashx")
	public String saveOrUpdateFenchang(Contract contract) {
		try{
			
			Staff sessionUser = this.getSessionStaff();			
 			if(contract.getcPIDContract().equals("-1")){
				contract.setCreatorID(sessionUser.getsId().toString());
				contract.setCreateTime(new Date());
				Contract contractFindBySerial = contractService.findBySerial(contract.getcPIDSerial());
				
				if(contractFindBySerial==null){
					Integer i = contractService.insertProjectAndClothesType(contract);
					if(i==1){
						return "nameconflict";  
					}					
				}else{
					return "conflict";  
				}				
				
			}else{
 
				/*ClothesType clothesType = new ClothesType();
				clothesType.setTypeCPIDSerial(contract.getcPIDSerial());
				Integer count = clothesTypeService.count(clothesType);
				if(count>0 && (!StringUtils.isEmpty(contract.getcPClothesTypes()))){
					return "clothestypeonlyone";
				}*/
				
				Contract contractFindBySerial = contractService.findBySerial(contract.getcPIDSerial());
				
				if(contractFindBySerial==null||contractFindBySerial.getcPIDContract().equals(contract.getcPIDContract())){
					
					contract.setModifierID (sessionUser.getsId().toString());
					contract.setModifyTime(new Date());
 					Integer i = contractService.updateProjectAndClothesType(contract);
 					if(i==1){
 						return "nameconflict";
 					}
					
				}else{
					return "conflict";  
				}
				
				

			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	
	@ResponseBody
	@RequestMapping(value = "/csharpclient.uploadprojectinfo.ashx")
	public HashMap<String,Object> uploadprojectinfo(@RequestBody String project) throws JSONException {
		HashMap<String,Object> resultMap = new HashMap<String,Object>();
		
		try{  
			
			JSONObject jsonObject = new JSONObject(project);
 			
			String projectname =  jsonObject.getString("projectname");
			String projectserial = jsonObject.getString("projectserial");
			Contract contract = new Contract();
			if(!(StringUtils.isEmpty(projectname)||StringUtils.isEmpty(projectserial))){
				
				contract.setcPName(projectname);
				contract.setcPIDSerial(projectserial);
			}
			else{
				resultMap.put("status", "0");
				resultMap.put("msg", "参数不全");
				return resultMap;
			}

	 			
	 			
			contract.setCreateTime(new Date());
			contract.setCreatorID("0");
			Contract contractFindBySerial = contractService.findBySerial(contract.getcPIDSerial());
			
			if(contractFindBySerial==null){
				contractService.insert(contract);
				resultMap.put("status", "1");
				resultMap.put("msg", "成功");
				return resultMap;
				
			}else{
				resultMap.put("status", "2");
				resultMap.put("msg", "项目代码重复");
				return resultMap;
			}
				
 			
	 	}catch(Exception e){
			log.error(getTrace(e));
  			resultMap.put("status", "0");
			resultMap.put("msg", "失败");
			return resultMap;
		} 
		
		
	     
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.list_all.ashx")
	 public List<Contract> listAll(Integer page,Integer rows,Contract contract) {

        Page p = new Page(0,0,contract);
		List<Contract> list = contractService.list(p);
	    return list;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.contractStatus.ashx")
	 public Map<String,Object> contractStatus(Integer page,Integer rows,Contract contract) {
 
	  //每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,contract);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			List<ContractStatus> list = contractService.listcontractStatus(p);
			Integer count = contractService.countcontractStatus(contract);
			for (ContractStatus contractStatus : list) {
				contractStatus.setContractCustomerProgressRatio(contractStatus.getContractDoneCustomer()+"/"+contractStatus.getContractTotalCustomer());
			}
					
			resultMap.put("total",count );//total键 存放总记录数，必须的  
			resultMap.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			resultMap.put("msg", "失败！");
			resultMap.put("status", "0");
		}
		
	    return resultMap;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.delContractFamily.ashx")
	 public Map<String,Object> delContractFamily(String pwd, String pid) {
 
	  
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			if(MD5Utils.md5(pwd).equals(this.getSessionStaff().getsPwd())){
				Contract contract = new Contract();
				contract.setModifierID(this.getSessionStaff().getsId().toString());
				contract.setModifyTime(new Date());
				contract.setcPIDSerial(pid);
				contractService.delContractFamily(contract);
				resultMap.put("msg", "删除成功！");
				resultMap.put("status", "1");
				
			}else{
				resultMap.put("msg", "密码错误！");
				resultMap.put("status", "0");
			}
			 
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			resultMap.put("msg", "失败！");
			resultMap.put("status", "0");
		}
		
	    return resultMap;  
	  }
	
	
	
	
	

}
